/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bsd5ex.group1.kaokang.dao;

import com.bsd5ex.group1.kaokang.config.TableNameConstants;
import com.bsd5ex.group1.kaokang.dbUtil.HibernateUtil;
import com.bsd5ex.group1.kaokang.model.Food;
import com.bsd5ex.group1.kaokang.model.Order;
import com.bsd5ex.group1.kaokang.model.OrderLineDetail;
import com.bsd5ex.group1.kaokang.model.OrderLineItem;
import com.bsd5ex.group1.kaokang.model.OrderLineItemType;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author VAIO
 */
public class OrderDAO extends AbstractDAO<Order> {

    public Order getOrderByID(int orderID) {
        return (Order) queryOneResult("from "+getEntityName()+" as o where o.id_order=" + orderID);
    }
    
    public static void main(String[] args) {
        
        FoodDAO foodDAO = new FoodDAO();
        List<Food> food = foodDAO.query("from Food where id=1");
        
        OrderLineItemTypeDAO litTypeDAO = new OrderLineItemTypeDAO();
        List<OrderLineItemType> oliType = litTypeDAO.query("from OrderLineItemType where id=1");
        
        //Create Object Heirachy
        
        Order order = new Order(new Date(), 35);
        
        OrderLineItem lineItem = new OrderLineItem();
        lineItem.setPrice(30);
        lineItem.setOrderLineItemType(oliType.get(0));
        lineItem.setOrder(order);
        
        OrderLineDetail detail1 = new OrderLineDetail();
        detail1.setFood( food.get(0) );
        detail1.setRatio(1.5f);
        detail1.setOrderLineItem(lineItem);
        
        //Save them all
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        
        session.save(order);
        session.refresh(order);
        
        session.save(lineItem);
        session.refresh(lineItem);
        
        session.save(detail1);
        tx.commit();
    }
    
}
